package com.cafuc.io;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.List;
import java.util.Properties;


public class FileOperation<list> {

    public void fileTest() {
        File  file = new File("C:\\Users\\gubler\\Desktop\\java.txt");
        System.out.println(file.exists());
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getName());
        File temp = new File("C:\\Users\\gubler\\Desktop\\1");
        if (!temp.exists()) {
            temp.mkdirs();
        }
    }

    public void filePathTest() {
        System.out.println(FileOperation.class.getResource("/test.txt").getPath());
        System.out.println(FileOperation.class.getClassLoader().getResource("test.txt").getPath());
    }

    public void readFile(String filePath) {
        InputStream is = null;
        BufferedReader br = null;
        int buffer = 0;
        StringBuffer sb;
        String line = "";
        try {
            // 使用 inputStream 读取文件
            is = new FileInputStream(filePath);
            buffer = 0;
            sb = new StringBuffer();
            while ((buffer = is.read()) != -1) {
                sb.append((char)buffer);
            }
            System.out.println(sb.toString());

            sb = new StringBuffer();;
            is = new FileInputStream(filePath);
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\r\n");
            }
            System.out.println(sb.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void writeFile(String filePath, String content) {
        OutputStream os = null;
        OutputStreamWriter osw = null;

        try {
            // 使用字节流写文件
            os = new FileOutputStream(filePath);
            os.write(content.getBytes("UTF-8"));
            os.flush();

            // 使用字符流写文件
            osw = new OutputStreamWriter(os, "UTF-8");
            osw.write(content, 0, content.length());
            osw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (osw != null) {
                    osw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void commonsIOTest(String filePath, String content) {
        try {
            List<String> lines = FileUtils.readLines(
                    new File(filePath), "UTF-8");
            System.out.println(lines);

            FileUtils.writeStringToFile(new File(filePath), content, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void propertiesTest(String filePath) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filePath));
            System.out.println(properties.get("jdbc.driverClassName"));
            System.out.println(properties.get("jdbc.url"));
            System.out.println(properties.get("jdbc.username"));
            System.out.println(properties.get("jdbc.password"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FileOperation fo = new FileOperation();
//        fo.fileTest();
//        fo.filePathTest();
        String filePath = "C:\\Users\\gubler\\Desktop\\java.txt";
        String content = "csdacdsac\ndscjdsc从r4324r是\n拉萨成都撒曹大43242432师";
//        fo.readFile(filePath);
//        fo.writeFile(filePath, content);
//        fo.commonsIOTest(filePath, content);
        fo.propertiesTest(
                FileOperation.class.getResource("/jdbc.properties").getPath());
    }
}